package com.grandtech.insurance.common.db.mongdb.factory;
import com.mongodb.MongoClientURI;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

public final class MongoFactory {

    private MongoFactory() {
    }

    public static MongoTemplate getSimpleMongoDbFactory(String uri) {
        if (uri == null) return null;
        try {
            MongoTemplate mongoTemplate;
            if (MongoClientConnect.getInstance().getMongoClientConnect(uri) == null) {
                MongoClientURI mongoClientURI = new MongoClientURI(uri);
                SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongoClientURI);
                mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
                MongoClientConnect.getInstance().cacheMongoClientConnect(uri, mongoTemplate);
            } else {
                mongoTemplate = MongoClientConnect.getInstance().getMongoClientConnect(uri);
            }
            return mongoTemplate;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
